'use strict';
app.controller('tableTypeCtrl', ['$scope','$state','toaster','$modal', '$log','utilService','tableTypeService', function($scope,$state,toaster,$modal,$log,utilService,tableTypeService) {
    //查询打印机列表
    $scope.findPageAll = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'table/type'
        }).then(function (result) {
        });
    }

    $scope.findPageAll();

    //打开添加窗口
    $scope.openAddPoup=function(){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/table/table_type_add.html',
            controller: 'tableTypeAddCtrl',
            size: 'md',
            scope:$scope,
            backdrop:'static'
        });

        modalInstance.result.then(function (result) {
            $scope.findPageAll();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }


    //打开修改窗口
    $scope.openEditPoup=function(id){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/table/table_type_edit.html',
            controller: 'tableTypeEditCtrl',
            size: 'md',
            scope:$scope,
            backdrop:'static',
            resolve:{
                data:function(){
                    return {id:id};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.findPageAll();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }

    $scope.delete= function(id, name){
        var param = {
            id:id,
            name:name
        };
        // query data.
        openModal("你是否要删除【" + name + "】桌号类型？");

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除桌号类型",
                controller:"tableTypeDelCtrl",
                message:msg,
                data:param
            });
        }
    }

}]);


app.controller('tableTypeAddCtrl', ['$scope','$modalInstance','$state','toaster','tableTypeService', function($scope, $modalInstance,$state,toaster,tableTypeService) {

    $scope.tableTypeAddVO={basicPeople:1,basicMoney:0,incrementPeople:1,incrementMoney:0,minNumber:0,maxNumber:0};

    var startTime=new Date();
    startTime.setHours(0);
    startTime.setMinutes(0);
    startTime.setSeconds(0);
    $scope.tableTypeAddVO.startTime=startTime;


    var endTime=new Date();
    endTime.setHours(1);
    endTime.setMinutes(0);
    endTime.setSeconds(0);
    $scope.tableTypeAddVO.endTime=endTime;

    $scope.addState=false;
    //查询品牌信息
    $scope.add = function () {
        if($scope.addState){
            return;
        }

        $scope.addState=true;

        tableTypeService.add($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.addState=false;
                    $modalInstance.close("success");
                    break;
                case 639:
                case 733:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };

}]);


app.controller('tableTypeEditCtrl', ['$scope','$modalInstance','$state','toaster','tableTypeService','data', function($scope, $modalInstance,$state,toaster,tableTypeService,data) {
    $scope.data=data;
    $scope.id=$scope.data.id;
    $scope.tableTypeEditVO={};

    $scope.editState=false;

    //
    $scope.findById = function () {
        tableTypeService.findById($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.tableTypeEditVO=result.data;


                    var startTime=new Date();
                    var startTimeArr=$scope.tableTypeEditVO.startTime.split(":");
                    startTime.setHours(startTimeArr[0]);
                    startTime.setMinutes(startTimeArr[1]);
                    startTime.setSeconds(startTimeArr[2]);


                    var endTime=new Date();
                    var endTimeArr=$scope.tableTypeEditVO.endTime.split(":");
                    endTime.setHours(endTimeArr[0]);
                    endTime.setMinutes(endTimeArr[1]);
                    endTime.setSeconds(endTimeArr[2]);



                    $scope.tableTypeEditVO.startTime=startTime;
                    $scope.tableTypeEditVO.endTime=endTime;
                    $scope.tableTypeEditVO.validTime=$scope.tableTypeEditVO.validTime/60;
                    break;
                case 731:
                case 733:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.findById();

    //查询品牌信息
    $scope.edit = function () {
        if($scope.editState){
            return;
        }

        $scope.editState=true;

        tableTypeService.edit($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.editState=false;
                    $modalInstance.close("success");
                    break;
                case 639:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };

}]);



app.controller('tableTypeDelCtrl', ['$scope', '$modalInstance','$state','tableTypeService','toaster','modalData',
    function($scope, $modalInstance,$state,tableTypeService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.id=$scope.modal.data.id;

        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            tableTypeService.updateToDelete($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.isDelModal=true;
                        $scope.findPageAll();
                        break;
                    case 731:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


app.controller('TimeCtrl', ['$scope', function($scope) {
    $scope.hstep = 1;
    $scope.mstep = 15;

    $scope.options = {
        hstep: [1, 2, 3],
        mstep: [1, 5, 10, 15, 25, 30]
    };

    $scope.ismeridian = false;
    $scope.toggleMode = function() {
        $scope.ismeridian = ! $scope.ismeridian;
    };
}]);